package org.hackystat.ui.projectviewer.gwt.client.widgets;

import org.hackystat.ui.projectviewer.gwt.client.CSSStyles;
import org.hackystat.ui.projectviewer.gwt.client.common.MenuButton;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.ClickListenerCollection;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.SourcesClickEvents;
import com.google.gwt.user.client.ui.Widget;

/**
 * Takes care about all possible project actions.
 *
 * @author Pavel Senin.
 *
 */
public class ProjectActionsWidget extends Composite implements ClickListener, SourcesClickEvents {

  /** {@literal} ACTION_VIEW the view action. */
  public static final String ACTION_VIEW = "view";
  /** {@literal} ACTION_NEW the new action. */
  public static final String ACTION_NEW = "new";
  /** {@literal} ACTION_EDIT the edit action. */
  public static final String ACTION_EDIT = "edit";
  /** {@literal} ACTION_DELETE the delete action. */
  public static final String ACTION_GETDATA = "getdata";
  /** {@literal} ACTION_CHART the delete action. */
  public static final String ACTION_CHART = "getcharts";
  /** {@literal} ACTION_DELETE the delete action. */
  public static final String ACTION_DELETE = "delete";
  /** {@literal} ACTION_DIARY the diary action. */
  public static final String ACTION_DIARY = "diary";

  /* The main outer pane */
  private final HorizontalPanel outer = new HorizontalPanel();
  private final HorizontalPanel inner = new HorizontalPanel();

  private static final MenuButton createProject = new MenuButton("new");
  private static final MenuButton editProject = new MenuButton("edit");
  private static final MenuButton getData = new MenuButton("data");
  private static final MenuButton getCharts = new MenuButton("charts");
  private static final MenuButton deleteProject = new MenuButton("delete");
  private static final MenuButton projectDiary = new MenuButton("diary");

  /* old selection handler, short term memory */
  private MenuButton oldSelectionHolder;
  private String action;

  private final ClickListenerCollection clickListeners = new ClickListenerCollection();

  /**
   * Does the menu construction.
   *
   */
  public ProjectActionsWidget() {
    outer.setStyleName(CSSStyles.ACTION_MENU_BAR);

    inner.setHorizontalAlignment(HorizontalPanel.ALIGN_LEFT);
    inner.add(createProject);
    inner.add(editProject);
    inner.add(getData);
    inner.add(getCharts);
    inner.add(projectDiary);
    inner.add(deleteProject);

    createProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    editProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    getData.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    getCharts.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    projectDiary.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    deleteProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);

    createProject.addClickListener(this);
    editProject.addClickListener(this);
    getData.addClickListener(this);
    getCharts.addClickListener(this);
    projectDiary.addClickListener(this);
    deleteProject.addClickListener(this);

    outer.add(inner);
    initWidget(outer);
  }

  /**
   * Menu click listener.
   *
   * @param sender The menu button clicked by user.
   */
  public void onClick(Widget sender) {

    if (sender instanceof MenuButton) {
      MenuButton mb = (MenuButton) sender;

      // "unselect" old selection
      if (null != oldSelectionHolder) {
        oldSelectionHolder.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
      }
      mb.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON_SELECTED);
      // memorize new selection
      oldSelectionHolder = mb;

      // create project
      if (mb.equals(createProject)) {
        this.action = ACTION_NEW;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_NEW");
        }
      }
      // edit project
      else if (mb.equals(editProject)) {
        this.action = ACTION_EDIT;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_EDIT");
        }
      }
      // get data
      else if (mb.equals(getData)) {
        this.action = ACTION_GETDATA;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_GETDATA");
        }
      }
      // get charts
      else if (mb.equals(getCharts)) {
        this.action = ACTION_CHART;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_CHART");
        }
      }
      // get diary
      else if (mb.equals(projectDiary)) {
        this.action = ACTION_DIARY;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_DIARY");
        }
      }
      // delete project
      else if (mb.equals(deleteProject)) {
        this.action = ACTION_DELETE;
        if (Log.isDebugEnabled()) {
          Log.debug("PAW: setting action ACTION_DELETE");
        }
      }
      clickListeners.fireClick(this);
    }
  }

  /**
   * Enables or disables buttons.
   *
   * @param b True for enabling, false for disabling.
   */
  public void setMode(boolean b) {
    createProject.setEnabled(b);
    editProject.setEnabled(b);
    getData.setEnabled(b);
    getCharts.setEnabled(b);
    projectDiary.setEnabled(b);
    deleteProject.setEnabled(b);
  }

  /**
   * Enables or disables Delete button.
   *
   * @param b True for enabling, false for disabling.
   */
  public void setDelete(boolean b) {
    deleteProject.setEnabled(b);
  }

  /**
   * Enables or disables Edit button.
   *
   * @param b True for enabling, false for disabling.
   */
  public void setEdit(boolean b) {
    editProject.setEnabled(b);
  }

  /**
   * {@inheritDoc}
   */
  public void addClickListener(ClickListener listener) {
    this.clickListeners.add(listener);
  }

  /**
   * {@inheritDoc}
   */
  public void removeClickListener(ClickListener listener) {
    this.clickListeners.remove(listener);
  }

  /**
   * Reports the last selected action.
   *
   * @return The last selected action.
   */
  public String getAction() {
    return this.action;
  }

  /**
   * Pops all pushed buttons.
   */
  public void refreshButtons() {
    createProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    editProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    getData.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    getCharts.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    projectDiary.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
    deleteProject.setStyleName(CSSStyles.PROJECT_LIST_WIDGET_BUTTON);
  }

}
